import router from './router'
import { useNProgress } from '@/hooks/web/useNProgress'
const { start, done } = useNProgress()

import { usePageLoading } from '@/hooks/web/usePageLoading'
const { loadStart, loadDone } = usePageLoading()

import { getAccessToken } from '@/utils/auth'

// 免登录白名单
const whiteList = [
  '/login'
]


router.beforeEach(async (to, from, next) => {
  start()
  loadStart()
  if (getAccessToken()) {
    if (to.path === '/login') {
      next({path: '/'})
    }
    next()
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
    }
  }
})

import { useTitle } from '@/hooks/web/useTitle'
router.afterEach((to) => {
  useTitle(to?.meta?.title as string)
  done() // 结束Progress
  loadDone()
})
